//
//  TestBSONAppDelegate.cpp
//  TestBSON
//
//  Created by mac on 14-11-18.
//  Copyright __MyCompanyName__ 2014年. All rights reserved.
//

#include "AppDelegate.h"

#include "cocos2d.h"
#include "SimpleAudioEngine.h"
#include "HelloWorldScene.h"

USING_NS_CC;
using namespace CocosDenshion;

#include "bson.h"

static void
test_bcon_new (void)
{
    bson_t expected;
    bson_t * bcon;
    
    bson_init (&expected);
    
    bson_append_utf8 (&expected, "hello", -1, "world", -1);
    bson_append_utf8 (&expected, "hello", -1, "asdfccaadf", -1);
    bson_append_utf8 (&expected, "hello", -1, "3rfasdf", -1);
    bson_append_utf8 (&expected, "hello", -1, "f", -1);
    
    BSON_APPEND_INT32(&expected, "intv", 223);
    BSON_APPEND_INT32(&expected, "intv", 223);
    BSON_APPEND_INT32(&expected, "intv", 223);
    BSON_APPEND_INT32(&expected, "intv", 223);
    
    
    //the same as expected
    bcon = BCON_NEW("hello", "world");
    
    bson_append_utf8 (bcon, "hello", -1, "world", -1);
    bson_append_utf8 (bcon, "hello", -1, "asdfccaadf", -1);
    bson_append_utf8 (bcon, "hello", -1, "3rfasdf", -1);
    bson_append_utf8 (bcon, "hello", -1, "f", -1);
    
    bson_append_utf8 (bcon, "intv", -1, "223", -1);
    bson_append_utf8 (bcon, "intv", -1, "223", -1);
    bson_append_utf8 (bcon, "intv", -1, "223", -1);
    bson_append_utf8 (bcon, "intv", -1, "223", -1);
    
    std::string test = bson_as_json(&expected, NULL);
    std::string test2 = bson_as_json(bcon, NULL);
    printf("test =%s json length=%d, bson length=%d\n",test.c_str(),test.length(),bcon->len);
    printf("--test =%s json length=%d, bson length=%d\n",test2.c_str(),test2.length(),bcon->len);
    
    //    bson_eq_bson (bcon, &expected);
    
    bson_destroy (bcon);
    bson_destroy (&expected);
}

AppDelegate::AppDelegate()
{
    //remember to define macro BSON_INSIDE
    test_bcon_new();
}

AppDelegate::~AppDelegate()
{
}

bool AppDelegate::applicationDidFinishLaunching()
{
    // initialize director
    CCDirector *pDirector = CCDirector::sharedDirector();
    pDirector->setOpenGLView(CCEGLView::sharedOpenGLView());

    // turn on display FPS
    pDirector->setDisplayStats(true);

    // set FPS. the default value is 1.0/60 if you don't call this
    pDirector->setAnimationInterval(1.0 / 60);

    // create a scene. it's an autorelease object
    CCScene *pScene = HelloWorld::scene();

    // run
    pDirector->runWithScene(pScene);

    return true;
}

// This function will be called when the app is inactive. When comes a phone call,it's be invoked too
void AppDelegate::applicationDidEnterBackground()
{
    CCDirector::sharedDirector()->stopAnimation();
    SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();
    SimpleAudioEngine::sharedEngine()->pauseAllEffects();
}

// this function will be called when the app is active again
void AppDelegate::applicationWillEnterForeground()
{
    CCDirector::sharedDirector()->startAnimation();
    SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();
    SimpleAudioEngine::sharedEngine()->resumeAllEffects();
}
